﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.utils.*;
    import Classes.*;
    import __AS3__.vec.*;
    import Engine.*;

    public class UpgradeDefinition {

        protected var m_oldItemName:String;
        protected var m_newItemName:String;
        protected var m_requirements:Requirements;
        protected var m_upgradeTrackingClass:String;
        protected var m_freeHarvestPhylum:String;
        protected var m_introSeenFlag:String;
        protected var m_introGuideNotify:String;
        protected var m_cashCost:int;
        protected var m_rewards:Dictionary;
        protected var m_helpers:Vector.<UpgradeHelperDefinition>;
        protected var m_firstTimeToasterText:String;
        protected var m_toasterIcon:String;
        protected var m_experiment:String;

        public function UpgradeDefinition(_arg1:String, _arg2:XML){
            if (((_arg1) && (_arg2))){
                this.m_oldItemName = _arg1;
                this.m_newItemName = String(_arg2.@item);
                this.m_cashCost = ((_arg2.@cashcost == null)) ? 0 : int(_arg2.@cashcost);
                this.m_rewards = this.parseRewards((_arg2.rewards as XMLList));
                this.m_helpers = this.parseHelpers((_arg2.helpers as XMLList));
                this.m_upgradeTrackingClass = String(_arg2.upgradeTrackingClass);
                this.m_freeHarvestPhylum = String(_arg2.freeHarvestPhylum);
                this.m_introSeenFlag = String(_arg2.introSeenFlag);
                this.m_introGuideNotify = String(_arg2.introGuideNotify);
                this.m_experiment = String(_arg2.experiment);
                this.m_firstTimeToasterText = String(_arg2.firstTimeToasterText);
                if (_arg2.toasterIcon){
                    this.m_toasterIcon = String(_arg2.toasterIcon);
                };
                if (_arg2.requirements.length() > 0){
                    this.m_requirements = new Requirements((_arg2.requirements as XMLList)[0]);
                };
            };
        }
        public static function getUpgradeChain(_arg1:String):Array{
            var _local2:Array = [];
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            do  {
                _local2.push(_local3.upgrade);
                _local3 = Global.gameSettings.getItemByName(_local3.upgrade.newItemName);
            } while (((_local3.upgrade) && (_local3.upgrade.isValid())));
            return (_local2);
        }
        public static function getFullUpgradeChain(_arg1:String):Array{
            var _local2:Array = [];
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            while (_local3) {
                _local2.push(_local3);
                if (((_local3.upgrade) && (_local3.upgrade.isValid()))){
                    _local3 = Global.gameSettings.getItemByName(_local3.upgrade.newItemName);
                } else {
                    _local3 = null;
                };
            };
            return (_local2);
        }
        public static function getUpgradeChainNames(_arg1:String):Array{
            var _local2:Array = getUpgradeChain(_arg1);
            var _local3:int;
            while (_local3 < _local2.length) {
                _local2[_local3] = (_local2[_local3] as UpgradeDefinition).m_newItemName;
                _local3++;
            };
            return (_local2);
        }
        public static function getFullUpgradeChainNames(_arg1:String):Array{
            var _local2:Array = getFullUpgradeChain(_arg1);
            var _local3:int;
            while (_local3 < _local2.length) {
                _local2[_local3] = (_local2[_local3] as Item).name;
                _local3++;
            };
            return (_local2);
        }
        public static function getNextUpgrade(_arg1:String):UpgradeDefinition{
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            if (((_local2.upgrade) && (_local2.upgrade.isValid()))){
                return (_local2.upgrade);
            };
            return (null);
        }

        private function parseRewards(_arg1:XMLList):Dictionary{
            var _local3:XML;
            var _local4:String;
            var _local5:Number;
            var _local2:Dictionary = new Dictionary();
            for each (_local3 in _arg1.children()) {
                _local4 = String(_local3.@type);
                _local5 = Number(_local3.@value);
                _local2[_local4] = _local5;
            };
            return (_local2);
        }
        private function parseHelpers(_arg1:XMLList):Vector.<UpgradeHelperDefinition>{
            var _local3:XML;
            var _local2:Vector.<UpgradeHelperDefinition> = new Vector.<UpgradeHelperDefinition>();
            for each (_local3 in _arg1.children()) {
                _local2.push(new UpgradeHelperDefinition(_local3));
            };
            return (_local2);
        }
        public function get rewards():Dictionary{
            return (this.m_rewards);
        }
        public function get oldItemName():String{
            return (this.m_oldItemName);
        }
        public function get newItemName():String{
            return (this.m_newItemName);
        }
        public function get requirements():Requirements{
            return (this.m_requirements);
        }
        public function get helpers():Vector.<UpgradeHelperDefinition>{
            return (this.m_helpers);
        }
        public function get cashCost():int{
            return (this.m_cashCost);
        }
        public function get upgradeTrackingClass():String{
            return (this.m_upgradeTrackingClass);
        }
        public function get freeHarvestPhylum():String{
            return (this.m_freeHarvestPhylum);
        }
        public function get introSeenFlag():String{
            return (this.m_introSeenFlag);
        }
        public function get introGuideNotify():String{
            return (this.m_introGuideNotify);
        }
        public function get firstTimeToasterText():String{
            return (this.m_firstTimeToasterText);
        }
        public function get toasterIcon():String{
            return (this.m_toasterIcon);
        }
        public function get experiment():String{
            return (this.m_experiment);
        }
        public function isValid():Boolean{
            return (((!((this.newItemName == null))) && (!((this.newItemName == "")))));
        }
        public function isUpgradePossible(_arg1:MapResource):Boolean{
            return (((!((this.m_requirements == null))) && (this.m_requirements.checkRequirements(_arg1))));
        }
        public function getUpgradeActionsProgress(_arg1:MapResource):Number{
            var _local2:String = this.m_requirements.getRequirementValue(Requirements.REQUIREMENT_UPGRADE_ACTIONS);
            if (_local2 == null){
                return (0);
            };
            var _local3:Number = Number(_local2);
            if (_local3 == 0){
                return (1);
            };
            var _local4:Number = _arg1.upgradeActions.getTotal();
            return (MathUtil.clamp((_local4 / _local3), 0, 1));
        }
        public function getUpgradeActionsRemaining(_arg1:MapResource):Number{
            var _local2:String = this.m_requirements.getRequirementValue(Requirements.REQUIREMENT_UPGRADE_ACTIONS);
            if (_local2 == null){
                return (0);
            };
            var _local3:Number = Number(_local2);
            if (_local3 == 0){
                return (1);
            };
            var _local4:Number = _arg1.upgradeActions.getTotal();
            return (Math.max((_local3 - _local4), 0));
        }

    }
}//package Classes.sim 
